﻿
''' <summary>
''' 端口状态
''' </summary>
''' <remarks></remarks>
Public Class PortStatus

    ''' <summary>
    ''' 端口号
    ''' </summary>
    ''' <remarks></remarks>
    Public ReadOnly PortID As Integer

    ''' <summary>
    ''' 端口所在设备的设备编号
    ''' </summary>
    ''' <remarks></remarks>
    Public ReadOnly DeviceID As Integer

    Private ReadOnly AlarmVisable As Boolean = True
    Private ReadOnly PortStatusType As DeviceStatusTypes

    Public Sub New(ByVal portItemId As Integer, ByVal devId As Integer, Optional ByVal HasAlarmStatus As Boolean = False)

        PortID = portItemId
        DeviceID = devId
        AlarmVisable = HasAlarmStatus
        PortStatusType = DeviceStatusTypes.Other

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。

        '' =======================
        '' 控制是否显示报警状态
        Me.lblAlarm.Visible = Not AlarmVisable

    End Sub

    Public Sub New(ByVal portItemId As Integer, ByVal devId As Integer, ByVal dst As DeviceStatusTypes, Optional ByVal HasAlarmStatus As Boolean = False)

        PortID = portItemId
        DeviceID = devId
        AlarmVisable = HasAlarmStatus
        PortStatusType = dst

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。

        '' =======================
        '' 控制是否显示报警状态
        Me.lblAlarm.Visible = Not AlarmVisable

    End Sub


    ''' <summary>
    ''' 端口类型
    ''' </summary>
    ''' <remarks></remarks>
    Private _PortType As PortTypes = PortTypes.Normal

    ''' <summary>
    ''' 端口名称
    ''' </summary>
    ''' <remarks></remarks>
    Private _portName As String

    ''' <summary>
    ''' 端口状态类型
    ''' </summary>
    ''' <remarks></remarks>
    Private _portStatus As PortStatuEnums = PortStatuEnums.Unknown

    ''' <summary>
    ''' 端口报警类型
    ''' </summary>
    ''' <remarks></remarks>
    Private _portAlarm As PortAlarmEnums = PortAlarmEnums.Unknown

    ''' <summary>
    ''' 状态的文本
    ''' </summary>
    ''' <remarks></remarks>
    Private _Status As String = "未知"

    ''' <summary>
    ''' 用于状态显示时的值
    ''' </summary>
    ''' <remarks>有可能是Int，float类型</remarks>
    Private _value As Object = Nothing

    ''' <summary>
    ''' 显示数据时的后缀
    ''' </summary>
    ''' <remarks></remarks>
    Private _suffix As String = ""

    ''' <summary>
    ''' 警告时显示的颜色
    ''' </summary>
    ''' <remarks></remarks>
    Private _color As Color = Color.Black

    ''' <summary>
    ''' 报警字符串
    ''' </summary>
    ''' <remarks></remarks>
    Private _Alarm As String = "未知"

    ''' <summary>
    ''' 端口名称
    ''' </summary>
    ''' <remarks></remarks>
    Property PortName() As String
        Get
            Return _portName
        End Get
        Set(ByVal value As String)
            _portName = value

            Me.UpdateValue()
        End Set
    End Property


#If COMPILE_VERSION = "TEST" Then
    Public ReadOnly Property AlarmStatusColor() As Color
        Get
            Return _color
        End Get
    End Property

    Public ReadOnly Property PortStatusContext() As String
        Get
            Return _Status
        End Get
    End Property
#End If

    ''' <summary>
    ''' 端口状态
    ''' </summary>
    ''' <remarks></remarks>
    Property PortStatus() As PortStatuEnums
        Get
            Return _portStatus
        End Get
        Set(ByVal value As PortStatuEnums)
            _portStatus = value

            ''同步状态
            Select Case _portStatus
                Case PortStatuEnums.Close
                    _Status = "关闭"
                    _Alarm = "未知"
                    _portAlarm = PortAlarmEnums.Unknown

                Case PortStatuEnums.Open
                    _Status = "打开"
                Case PortStatuEnums.Number
                    If _value = Nothing Then
                        _Status = "0" + Me._suffix

                    Else
                        _Status = _value.ToString()
                    End If
                Case PortStatuEnums.Invalid
                    _Status = "无效"
                Case PortStatuEnums.Unknown
                    _Status = "未知"
                    _Alarm = "未知"
                    _portAlarm = PortAlarmEnums.Unknown
                Case Else
                    _Status = "未知"
                    _Alarm = "未知"
                    _portAlarm = PortAlarmEnums.Unknown
            End Select

            Select Case Me.PortStatusType
                Case DeviceStatusTypes.LostPower
                    If (_portStatus = PortStatuEnums.Open) Then
                        _Status = "断电"
                    ElseIf _portStatus = PortStatuEnums.Unknown Then
                        _Status = "未知"
                    Else
                        _Status = "供电"
                    End If
                Case DeviceStatusTypes.StartFailure
                    If (_portStatus = PortStatuEnums.Open) Then
                        _Status = "开启"
                    ElseIf _portStatus = PortStatuEnums.Unknown Then
                        _Status = "未知"
                    Else
                        _Status = "停止"
                    End If
                Case DeviceStatusTypes.Other
                    ''============================
                    '' 为支持 机头的运行状态
                    If AlarmVisable Then
                        If (_portStatus = PortStatuEnums.Open) Then
                            _Status = "运行"
                        ElseIf (_portStatus = PortStatuEnums.Unknown) Then
                            _Status = "未知"
                        Else
                            _Status = "停止"
                        End If
                    End If
            End Select
            Me.UpdateValue()

        End Set
    End Property


    ''' <summary>
    ''' 设置控件状态显示的值
    ''' </summary>
    ''' <remarks></remarks>
    Property Value() As Object
        Get
            Return _value
        End Get
        Set(ByVal value As Object)
            If (value <> Nothing) Then
                _value = value

                Try
                    Dim fvalue As Single = (Convert.ToSingle(value))
                    If (fvalue < -299) Then
                        _Status = "##.## " + Me._suffix
                    Else
                        _Status = (Convert.ToInt32(CSng(value) * 100) * 1.0 / 100.0).ToString("#.00") + " " + Me._suffix
                    End If

                Catch ex As Exception
                    System.Diagnostics.Debug.Print(ex.Message)
                    _Status = "##.## " + Me._suffix
                End Try

                Me.UpdateValue()
            End If
        End Set
    End Property

    ''' <summary>
    ''' 设置或获取报警状态
    ''' </summary>
    ''' <remarks></remarks>
    Property PortAlarmStatus() As PortAlarmEnums
        Get
            Return _portAlarm
        End Get
        Set(ByVal value As PortAlarmEnums)
            _portAlarm = value
            lblAlarm.Font = New Font(lblAlarm.Font.FontFamily, lblAlarm.Font.Size, FontStyle.Regular)
            Select Case _portAlarm
                Case PortAlarmEnums.Normal
                    _color = Color.Black
                    _Alarm = "正常"
                Case PortAlarmEnums.PreAlarm
                    _color = Color.OrangeRed
                    _Alarm = "预警"
                Case PortAlarmEnums.Alarm
                    _color = Color.Red
                    _Alarm = "报警"
                    lblAlarm.Font = New Font(lblAlarm.Font.FontFamily, lblAlarm.Font.Size, FontStyle.Bold)
                Case PortAlarmEnums.Invalid
                    _color = Color.Black
                    _Alarm = "无效"
                Case PortAlarmEnums.Unknown
                    _color = Color.Black
                    _Alarm = "未知"
            End Select
            UpdateValue()
        End Set
    End Property

    ''' <summary>
    ''' 端口类型
    ''' </summary>
    ''' <remarks></remarks>
    Property PortType() As PortTypes
        Get
            Return _PortType
        End Get
        Set(ByVal value As PortTypes)
            If (_PortType <> value) Then
                _PortType = value
                Select Case _PortType
                    Case PortTypes.Normal
                        _suffix = ""
                    Case PortTypes.Temperature
                        _suffix = "℃"
                    Case PortTypes.Humidity
                        _suffix = "%"
                    Case Else
                        _suffix = ""
                End Select

                UpdateValue()
            End If

        End Set
    End Property

    ''' <summary>
    ''' 更新界面内容
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub UpdateValue()

        ''=============================
        '' 更新报警

        If Me.lblAlarm.InvokeRequired Then
            Me.lblAlarm.Invoke(New up(AddressOf UpdateAlarm))
        Else
            Me.lblAlarm.Text = _Alarm
            Me.lblAlarm.ForeColor = _color
        End If


        ''==============================
        '' 更新状态
        If (lblStatus.InvokeRequired) Then
            Me.lblStatus.Invoke(New up(AddressOf UpdateStatus))
        Else
            Me.lblStatus.Text = _Status
        End If

        ''==============================
        '' 更新名称
        If (lblName.InvokeRequired) Then
            Me.lblName.Invoke(New up(AddressOf UpdateName))
        Else
            Me.lblName.Text = _portName
        End If


    End Sub

    Private Delegate Sub up()

    Private Sub UpdateAlarm()
        Me.lblAlarm.ForeColor = _color
        lblAlarm.Text = _Alarm
    End Sub

    Private Sub UpdateStatus()
        lblStatus.Text = _Status
    End Sub

    Private Sub UpdateName()
        lblName.Text = _portName
    End Sub

    ''' <summary>
    ''' 更新端口状态和报警状态
    ''' </summary>
    ''' <param name="strStatus">端口状态</param>
    ''' <param name="strAlarm">报警状态</param>
    ''' <remarks></remarks>
    Public Sub UpdateValues(ByVal strStatus As String, ByVal strAlarm As String)
        lblAlarm.Font = New Font(lblAlarm.Font.FontFamily, lblAlarm.Font.Size, FontStyle.Regular)
        If (strAlarm = "报警") Then
            Me._color = Color.Red
            lblAlarm.Font = New Font(lblAlarm.Font.FontFamily, lblAlarm.Font.Size, FontStyle.Bold)
        ElseIf (strAlarm = "预警") Then
            Me._color = Color.OrangeRed
        Else
            Me._color = Color.Black
        End If

        Me._Alarm = strAlarm
        Me._Status = strStatus

        Me.lblAlarm.ForeColor = _color

        Me.lblAlarm.Text = _Alarm

        Me.lblStatus.Text = _Status
    End Sub


    ''' <summary>
    ''' 更新端口状态和报警状态
    ''' </summary>
    ''' <param name="val ">端口状态值</param>
    ''' <param name="strAlarm">报警状态</param>
    ''' <remarks></remarks>
    Public Sub UpdateValues(ByVal val As Object, ByVal strAlarm As String)
        lblAlarm.Font = New Font(lblAlarm.Font.FontFamily, lblAlarm.Font.Size, FontStyle.Regular)
        If (strAlarm = "报警") Then
            Me._color = Color.Red
            lblAlarm.Font = New Font(lblAlarm.Font.FontFamily, lblAlarm.Font.Size, FontStyle.Bold)
        ElseIf (strAlarm = "预警") Then
            Me._color = Color.OrangeRed
        Else
            Me._color = Color.Black
        End If

        Me._Alarm = strAlarm
        Me._Status = val.ToString() + " " + Me._suffix

        Me.lblAlarm.ForeColor = _color

        Me.lblAlarm.Text = _Alarm

        Me.lblStatus.Text = _Status
    End Sub

    ''' <summary>
    ''' 更新端口状态和报警状态
    ''' </summary>
    ''' <param name="val ">端口状态值</param>
    ''' <param name="vAlarm">报警状态</param>
    ''' <remarks></remarks>
    Public Sub UpdateValue(ByVal val As Object, ByVal vAlarm As PortAlarmEnums)
        Me.Value = val
        Me.PortAlarmStatus = vAlarm

        Me.UpdateValue()
    End Sub

    ''' <summary>
    ''' 更新端口状态和报警状态
    ''' </summary>
    ''' <param name="vStatus ">端口状态值</param>
    ''' <param name="vAlarm">报警状态</param>
    ''' <remarks></remarks>
    Public Sub UpdateValue(ByVal vStatus As PortStatuEnums, ByVal vAlarm As PortAlarmEnums)
        Me.PortStatus = vStatus
        Me.PortAlarmStatus = vAlarm

        Me.UpdateValue()
    End Sub

    ''' <summary>
    ''' 覆盖Text
    ''' </summary>
    ''' <remarks></remarks>
    Public Shadows Function Text() As String
        Return Me.PortName
    End Function

    ''' <summary>
    ''' 覆盖Tostring()
    ''' </summary>
    Public Overrides Function ToString() As String
        Return Me.PortName
    End Function
End Class


''' <summary>
''' 端口开/关状态
''' </summary>
''' <remarks></remarks>
Public Enum PortStatuEnums

    ''' <summary>
    ''' 处于关闭状态
    ''' </summary>
    ''' <remarks></remarks>
    Close = 0

    ''' <summary>
    ''' 处于打开状态
    ''' </summary>
    ''' <remarks></remarks>
    Open = 1

    ''' <summary>
    ''' 数字类型显示状态
    ''' </summary>
    ''' <remarks></remarks>
    Number = 2
    ''' <summary>
    ''' 无效
    ''' </summary>
    ''' <remarks></remarks>
    Invalid = 3

    '''<summary >未知</summary>
    ''' <remarks></remarks>
    Unknown = 4
End Enum

''' <summary>
''' 端口报警状态
''' </summary>
''' <remarks></remarks>
Public Enum PortAlarmEnums
    ''' <summary>
    ''' 正常
    ''' </summary>
    ''' <remarks></remarks>
    Normal = 2
    ''' <summary>
    ''' 预警
    ''' </summary>
    ''' <remarks></remarks>
    PreAlarm = 0

    ''' <summary>
    ''' 报警
    ''' </summary>
    ''' <remarks></remarks>
    Alarm = 1


    ''' <summary>
    ''' 无效
    ''' </summary>
    ''' <remarks></remarks>
    Invalid = 3
    ''' <summary>
    ''' 未知
    ''' </summary>
    ''' <remarks></remarks>
    Unknown = 4

End Enum

''' <summary>
''' 端口类型
''' </summary>
''' <remarks></remarks>
Public Enum PortTypes
    ''' <summary>
    ''' 普通
    ''' </summary>
    ''' <remarks></remarks>
    Normal = 0

    ''' <summary>
    ''' 温度
    ''' </summary>
    ''' <remarks></remarks>
    Temperature = 1
    ''' <summary>
    ''' 湿度
    ''' </summary>
    ''' <remarks></remarks>
    Humidity = 2
End Enum

Public Enum DeviceStatusTypes
    ''' <summary>
    ''' 断电
    ''' </summary>
    ''' <remarks></remarks>
    LostPower = 1

    ''' <summary>
    ''' 启动故障
    ''' </summary>
    ''' <remarks></remarks>
    StartFailure = 2
    ''' <summary>
    ''' 其它
    ''' </summary>
    ''' <remarks></remarks>
    Other = 3
End Enum